capture log close
clear all
set maxvar 10000
set more off
pause off
# delimit;

local log "";
local crime "";
local output "";


/* 
Prep 4, collapse data (exactage)
*/


local fs_vars  "age18_yes n_age18unfav n_onssi1922 n_onssi1922_exact n_onssi20" ;
local hh_vars "n_earnhh9702 n_earnhh9712 n_emphhpost9702 n_emphhpost9712 n_inchh9702 n_inchh9712" ;
local covariates "n_male n_firstage n_fam_singmom n_fam_nopar n_dibmdr_mie n_dibmdr_mine n_dibmdr_miss n_diag1_mental n_diag1_nerv n_diag1_sense n_diag1_infec n_diag1_endo n_diag1_none n_diag1_cong n_diag1_musc n_diag1_resp n_diag1_blood n_diag1_neo n_earnhhpre9095" ;
local main_vars "n_crime* c_crime*" ; 
local demo_list "all " ; 
local controls_all "n_male i.n_firstage i.n_diag1cat n_dibmdr_mie n_dibmdr_mine n_dibmdr_miss n_fam_singmom n_fam_nopar i.n_earnhhbins i.state" ; 
local bwidth = 264 ;


foreach demo in `demo_list' { ;

	use "`crime'\prep2\crime_prwora_prepforreg_exactage.dta", clear ;
	
	/* Subsamples */
	gen demo_all = 1 ;
	gen byte demo_othment = (n_diag1_mental == 1 & (substr(dibdig,1,3) != "317" & substr(dibdig,1,3) != "318" & substr(dibdig,1,3) != "319" )) ;
	gen byte demo_nonment = (demo_othment == 0) ;
	gen byte demo_twopar = (n_fam_singmom != 1 & n_fam_nopar != 1) ;
	gen byte demo_singno = (n_fam_singmom == 1) | (n_fam_nopar ==1) ;
	gen byte demo_nopar  = (n_fam_nopar == 1) ;
	gen byte demo_male 	 = (n_male ==1) ;
	gen byte demo_female = (n_male !=1) ;

	keep if demo_`demo' == 1 ;

	/* Replace covariates and FS vars with missing if not in main sample */
	foreach var of varlist 
		 `covariates' `fs_vars' `hh_vars'
		{ ;
			replace `var' = `var' if keep_1997_2017_2c == 1 ;
		} ;

	summ dobwk_run ;
	local dobwk_max = r(max) ;
	local dobwk_min = r(min)*-1 ;
	gen dob2wk_run = . ;
	forval wk = 0(2)`dobwk_max' { ;
		replace dob2wk_run = (`wk'+`=`wk'+1')/2 if dobwk_run == `wk' | dobwk_run == `=`wk'+1' ;
	} ;
	forval wk = 1(2)`dobwk_min' { ;
		replace dob2wk_run = -(`wk'+`=`wk'+1')/2 if dobwk_run == -`wk' | dobwk_run == -`=`wk'+1' ;
	} ;
	tab dobwk_run, m ;
	tab dob2wk_run, m ;
	gen dob2wk_stata = dobwk_stata ;
	
	foreach var of varlist 
			`main_vars' `hh_vars' `fs_vars'
			{ ;
			xi: reg `var' `controls_all' if abs(dobdd_run) <= `bwidth' ;
			predict r_`var', r ;
			} ;
		

	foreach time in wk 2wk mo { ;

		preserve;
		
		foreach var of varlist 
			dobdd_run dob`time'_stata 
			`main_vars' `covariates' `hh_vars' `fs_vars' 
			r_*
			{ ;
			gen N`var' = `var' ;
			} ;
		
		collapse	(mean)	
							dobdd_run dob`time'_stata
							`main_vars' `covariates' `hh_vars' `fs_vars' 
							r_*
					(count) N* , 
			by(dob`time'_run) ;
			
		foreach var of varlist 
			dobdd_run dob`time'_stata 
			`main_vars' `covariates' `hh_vars' `fs_vars' 
			r_*
			{ ;
			replace `var' = . if N`var' < 10 ;
			} ;
		
		rename Ndob`time'_stata count ;
			
		save "`output'\prwora_crime_collapse`time'_`demo'_exactage.dta", replace ;	
		restore ;
	} ; /* end time */
	
} ; /* end demo */

capture log close;
